package com.example.sunmiv1.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.utils.CrashHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class BugService extends Service {

    private ACache aCache;
    private Context context;

    public BugService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        aCache = ACache.get(this);
        context = this;
        /*ThreadManager.getSinglePool().execute(new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();
                timer.schedule(new Work(), 0, 60000);
            }
        });*/
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public static void Uploadbug(final Context context, final String bugs) {
//                Looper.prepare();
        final ACache aCache = ACache.get(context);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("sunmiv1:", "phone=" + aCache.getAsString("login_acnum") +
                    ";pass=" + aCache.getAsString("login_pass") +
                    ";url=" + aCache.getAsString("url") + bugs);
            Log.e("上傳bug的接口上傳的數據=", jsonObject + "");
//            HttpUtils.post(context, "https://www.mobzhifu.com/index.php/Android/Food/casherLog", jsonObject, new TextCallBack() {
//                @Override
//                protected void onSuccess(String text) {
//                    Log.e("上傳bug的接口返回的數據=", text);
//                    File file = new File("/mnt/sdcard/crash/");
//                    if (!file.exists()) {
//                        file.mkdirs();
//                    }
//                    CrashHandler.deleteFile(file);
//                    aCache.remove("log");
//                    aCache.put("log", "");
//                }
//
//                @Override
//                protected void onFailure(ResponseException e) {
//
//                }
//            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
//                Looper.loop();
    }

}
